﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Threading;
using System.Data.SqlClient;

namespace For_FS_2013_1_User
{
    public class FileManager
    {
        private const string directoryPath = @"C:\Users\Michael13\Desktop\Files FS-2013\";
        
        public FileManager()
        {
            ReloadFiles();
        }

        private void LoadFilesToDB()
        {
            string[] files=Directory.GetFiles(directoryPath);
            foreach (string str in files)
            {
                Program.users.ExecuteSQLCommand("INSERT into Files(Name, Path) values('"+Path.GetFileName(str)+"', '"+directoryPath+"')").Close();
            }
        }

        private void ClearFileDB()
        {
            Program.users.ExecuteSQLCommand("TRUNCATE table Files").Close();
        }

        public void ReloadFiles()
        {
            ClearFileDB();
            LoadFilesToDB();
        }

        public bool ChangeFileAccess(string fileName)
        {
            if (ContainsFile(fileName))
            {
                SqlDataReader sr = Program.users.ExecuteSQLCommand("Select AvailableForUser from Files WHERE Name='" + fileName + "'");
                while (sr.Read())
                    if (sr["AvailableForUser"].ToString() == "1")
                    {
                        sr.Close();
                        Program.users.ExecuteSQLCommand("UPDATE Files SET AvailableForUser=0 WHERE Name='" + fileName + "'").Close();
                    }
                    else
                    {
                        sr.Close();
                        Program.users.ExecuteSQLCommand("UPDATE Files SET AvailableForUser=1 WHERE Name='" + fileName + "'").Close();
                    }
                return true;
            }
            return false;
        }

        public bool ContainsFile(string fileName)
        {
            SqlDataReader sr = Program.users.ExecuteSQLCommand("Select Name from Files WHERE Name='" + fileName + "'");
            while (sr.Read())
                if (sr["Name"].ToString() == fileName)
                {
                    sr.Close();
                    return true;
                }
            sr.Close();
            return false;
        }

        public HashSet<string> GetFiles()
        {
            HashSet<string> result = new HashSet<string>();
            ReloadFiles();
            
                SqlDataReader sr = Program.users.ExecuteSQLCommand("Select Name from Files where AvailableForUser=1");
                while (sr.Read())
                    result.Add(sr["Name"].ToString());
                sr.Close();
            return result;
        }

        public string GetFilePath(string fileName)
        {
            if (ContainsFile(fileName))
            {
                SqlDataReader sr = Program.users.ExecuteSQLCommand("Select Path, Name from Files WHERE Name='" + fileName + "'");
                while (sr.Read())
                    if (sr["Name"].ToString() == fileName)
                    {
                        string path = sr["Path"].ToString();
                        sr.Close();
                        return path;
                    }
            }
            return null;
        }
    }
}
